﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Platformer
{
    public class Bullet
    {      
        // Useful properties
        protected Texture2D texture;
        protected PlatformerGame game;

        // Position of the bullet
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
        protected Vector2 position;

        // Direction of the bullet
        public Vector2 Direction
        {
            get { return direction; }
            set { direction = value; }
        }
        protected Vector2 direction;

        // Speed of the bullet
        public float Speed
        {
            get { return speed; }
            set { speed = value; }
        }
        protected float speed;

        // Size of the bullet
        public Point Size
        {
            get { return size; }
        }
        protected Point size;

        // Rectangle depicting the bullet (used for collision)
        public Rectangle Rect
        {
            get { return rect; }
        }
        protected Rectangle rect;

        // When the bullet should be deleted
        public bool Wasted
        {
            get { return wasted; }
            set { wasted = value; }
        }
        protected bool wasted = false;

        public Bullet(PlatformerGame game, Vector2 position, Vector2 direction)
        {
            this.game = game;
            this.direction = direction;
            this.position = position;

            // Just to prevent errors
            texture = game.Content.Load<Texture2D>("Bullets/bullet1");        
        }

        public virtual void Update()
        {
          //  CheckWasted();
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, Color.White);
        }

        /* Custom */
        protected void CheckWasted()
        {
            if (wasted)
                return;

            RectUpdate();
            OutOfScreen();
            DoodadCollision();
        }

        void RectUpdate()
        {
            rect = new Rectangle((int)position.X, (int)position.Y, size.X, size.Y);
        }

        protected void OutOfScreen()
        {
            if(position.X > PlatformerGame.WIDTH)
                wasted = true;
            else if(position.X + size.X < 0)
                wasted = true;

            if(position.Y > PlatformerGame.HEIGHT)
                wasted = true;
            else if(position.Y + size.Y < 0)
                wasted = true;
        }

        protected void DoodadCollision()
        {
            foreach (Block item in game.BlocksList)
            {
                if (RectangleCollision(item))
                    wasted = true;
            }
        }

        // Check simple rectangle collision
        protected bool RectangleCollision(Block obj)
        {
            if (rect.Intersects(obj.Rect))
                return true;

            return false;
        }

        protected void BasicMovement()
        {
            position += direction * speed;
        }
    }
}
